/// <reference name="MicrosoftAjax.js" />
/// <reference name="dnn.js" assembly="DotNetNuke.WebUtility" />
/// <reference name="dnn.xmlhttp.js" assembly="DotNetNuke.WebUtility" />

Type.registerNamespace('OliverHine');
    
OliverHine.EditQuicksand = function()
{
    //Call Base Method
    OliverHine.EditQuicksand.initializeBase(this);
    //Member Variables
    this._msgs = {};
    this._settings = {};
    this._updateButton = null;

    //Associated delegates to single member variable dictionary to make it easy to dispose
    this._delegates = {
        _updateSuccessDelegate: Function.createDelegate(this, this._updateSuccess),
        _updateFailDelegate: Function.createDelegate(this, this._updateFail),
        _onLoadDelegate: Function.createDelegate(this, this._onLoad)
        };

    //Event Hookup
    Sys.Application.add_load(this._delegates._onLoadDelegate);
}

OliverHine.EditQuicksand.prototype =
{
    //Properties
    get_ns: function () { return this.get_id() + '_'; },
    get_msgs: function () { return this._msgs; },
    set_msgs: function (value) { this._msgs = value; },
    get_settings: function () { return this._settings; },
    set_settings: function (value) { this._settings = value; },

    //Events
    initialize: function () {
        //Call Base Method    
        OliverHine.EditQuicksand.callBaseMethod(this, 'initialize');

        //hookup event handlers
        this._updateButton = $get(this.get_ns() + 'cmdUpdate');
        $addHandlers(this._updateButton, { "click": this._onUpdate }, this);
    },

    _onLoad: function (src, args) {
        this._updateControls();
    },

    _onUpdate: function (src, arg) {
        this._displayWait(true);
        this._updateSettings();
        dnn.xmlhttp.callControlMethod('OliverHine.Modules.Quicksand.EditQuicksand.' + this.get_id(),
            'UpdateSettings', { Settings: this._settings }, this._delegates._updateSuccessDelegate, this._delegates._updateFailDelegate);
    },

    //Methods
    getMessage: function (key) {
        return this._msgs[key];
    },

    getSetting: function (key, def) {
        if (this._settings[key])
            return this._settings[key];
        return def;
    },

    //Private Methods
    _updateControls: function () {
        //$get(this.get_ns() + 'ParentTabDropDownList').value = this.getSetting('ParentTabID', '');
        //$get(this.get_ns() + 'txtDesc').value = this.getSetting('Desc', '');
    },

    _updateSettings: function () {
        this._settings['ParentTabID'] = $get(this.get_ns() + 'ParentTabDropDownList').value;
        this._settings['ShowHiddenTabs'] = $get(this.get_ns() + 'ShowHiddenTabsCheckbox').checked;
    },

    _displayWait: function (show) {
        $get(this.get_ns() + 'imgAjax').className = (show ? '' : 'ceHidden');
    },

    _updateSuccess: function (payload, ctx, req) {
        this._displayWait(false);
        alert(this.getMessage('SettingsSaved'));
    },

    _updateFail: function (payload, ctx, req) {
        this._displayWait(false);
        alert('error: ' + payload);
    },

    dispose: function () {
        $clearHandlers(this._updateButton);
        this._updateButton = null;
        this._delegates = null;
        OliverHine.EditQuicksand.callBaseMethod(this, 'dispose');
    }
}

//register class and inherit from Sys.Component
OliverHine.EditQuicksand.registerClass('OliverHine.EditQuicksand', Sys.Component);
